package com.hoopeu.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hoopeu.smarthome.R;

public class RoomAdapter extends BaseAdapter{

	private Context mContext;
	private List<String> mDataList;
	
	private OnRoomItemClickListener mLishener;
	public RoomAdapter(Context context) {
		// TODO Auto-generated constructor stub
		this.mContext = context;
	}
	
	public void setData(List<String> dataList)
	{
		this.mDataList = dataList;
	}
	
	public void setOnRoomItemClickLishener(OnRoomItemClickListener l){
		mLishener = l;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(this.mDataList!=null)
			return this.mDataList.size();
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if(this.mDataList!=null)
			return this.mDataList.get(position);
		return "";
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;
		ViewHolder holder = null;
		if(view == null)
		{
			holder = new ViewHolder();
			view = LayoutInflater.from(mContext).inflate(R.layout.room_item_layout, parent,false);
			
			holder.roomHeader = (ImageView)view.findViewById(R.id.iv_room_header);
			holder.roomName = (TextView)view.findViewById(R.id.tv_room_name);
			
			String name = "";
			if(this.mDataList!=null)
				name = this.mDataList.get(position);
			holder.roomName.setText(name);
			view.setTag(holder);
			
		}
		else
			holder = (ViewHolder)view.getTag();
		
		view.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mLishener.onItemClick(v, position);
			}
		});
		return view;
	}

	
	class ViewHolder
	{
		ImageView roomHeader;
		TextView roomName;
	}
	
	public interface OnRoomItemClickListener
	{
		public abstract void onItemClick(View view,int index);
	}
}
